<script setup lang="ts">
import { requiredCheck, validDomian } from "@/utils/verify";
import { ref, onMounted } from "vue";
import { addData, editData } from "@/api/modules/system/account";
import { cfgSettime, cfgGettime } from "@/api/modules/system/params";
import { ElMessage } from "element-plus";

interface Props {
  title?: string;
  rowData?: Object;
}

const props = withDefaults(defineProps<Props>(), {
  title: "新增账号",
  rowData: undefined
});
defineOptions({
  name: "UpdateForm" // 默认策略
});
import useUpdate from "@/hooks/useUpdate";
import { ifaceAll, ifacemodeUpdate } from "@/api/modules/dynamic-camouflage/line-config";
import BaseButton from "@/components/BaseButton/index.vue";

const emits = defineEmits(["success"]);
const { updateFormData, updateFormRef, updateFormRules, submit, showHandle, openHandle, closeHandle } = useUpdate({
  rules: {
    ntp_server: [...requiredCheck("请输入"),{ validator: validDomian }],
    day: [...requiredCheck("请输入频率")]
  },
  defaultData: {
    type: 1,
    uinx_time: new Date()
  }
});
const ifaces = ref([]);

async function getIface() {
  try {
    const res = await ifaceAll();
    console.log(res);
    if (res) {
      ifaces.value = Object.values(res);
      console.log(ifaces.value);
    }
    Object.values(res);
  } catch (error) {}
}

async function submitCb() {
  await submit({
    addApi: addData,
    updateApi: editData,
    sendData: {
      name: updateFormData.name,
      passwordRe: updateFormData.passwordRe || "",
      password: updateFormData.password || "",
      role: updateFormData.role,
      id: updateFormData?.id
    }
  });
  emits("success");
}

async function getCfg() {
  const res = await cfgGettime();
  console.log(res);
  updateFormData.type = res.type;
  updateFormData.uinx_time = new Date(res.uinx_time * 1000);
  updateFormData.ntp_server = res?.ntp_server || "";
  updateFormData.day = res?.day || undefined;
}

async function setCfg() {
  await updateFormRef.value.validate();
  await cfgSettime({
    uinx_time: updateFormData.type == 1 ? parseInt(new Date(updateFormData.uinx_time).getTime() / 1000) : undefined,
    type: updateFormData.type,
    ntp_server: updateFormData.type == 2 ? updateFormData?.ntp_server : undefined,
    day: updateFormData.type == 2 ? updateFormData?.day : undefined
  });
  ElMessage.success("更新成功");
  getCfg();
}

onMounted(() => {
  getCfg();
});
</script>

<template>
  <el-card>
    <template #header>
      <div class="card-header h-box justify-between">
        <span>系统时间</span>
        <div class="card-footer h-box justify-end">
          <base-button @click="getCfg" type="refresh_icon_text"></base-button>
          <base-button @click="setCfg" type="save_icon"></base-button>
        </div>
      </div>
    </template>

    <el-form ref="updateFormRef" :model="updateFormData" :rules="updateFormRules" label-width="85px" label-suffix="：">
      <el-form-item label="类型">
        <el-select v-model="updateFormData.type" style="width: 180px">
          <el-option label="自定义" :value="1"></el-option>
          <el-option label="网络" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="系统时间" v-if="updateFormData.type == 1">
        <el-date-picker
          style="width: 180px"
          :clearable="false"
          v-model="updateFormData.uinx_time"
          type="datetime"
          placeholder="请选择"
        />
      </el-form-item>
      <el-form-item label="地址" v-if="updateFormData.type == 2" prop="ntp_server">
        <el-input style="width: 180px" placeholder="请输入" v-model="updateFormData.ntp_server"></el-input>
      </el-form-item>
      <el-form-item label="频率" v-if="updateFormData.type == 2" prop="day">
        <el-input-number style="width:180px" :min="0" :step="1" :precision="0" placeholder="请输入" v-model="updateFormData.day"></el-input-number>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<style lang="scss" scoped></style>
